# -*- coding: utf-8 -*-
"""
@Time: 2021/7/2 15:35
@Author: 鹄望潇湘
@File: Main.py.py
@desc: 教改和精品课项目预算数据格式化
"""


class Item:
    def __init__(self, name: str = "", value: float = 0):
        self.name = name
        self.value = value

    def toString(self) -> str:
        if self.value == 0:
            return ""
        else:
            return self.name+": "+str(self.value)+"万元，"

    def changeValue(self, newValue: float):
        self.value = newValue


class Project:
    def __init__(self, limit: float=0):
        self.limit = limit
        self.items = []

    def addItem(self, item: Item):
        self.items.append(item)

    def sum(self) -> float:
        result = 0
        for item in self.items:
            result = result + item.value
        return result

    def toString(self) -> str:
        sum = 0
        for item in self.items:
            sum = sum + item.value
        result = ""
        sum2 = 0
        for item in self.items:
            target_value = round(self.limit*item.value/sum, 2)
            item.changeValue(target_value)
            sum2 = sum2 + target_value

        if sum2 != self.limit:
            delta = self.limit - sum2
            for item in self.items:
                if item.value != 0 and item.value + delta > 0:
                    item.changeValue(round(item.value + delta, 2))
                    break
        for item in self.items:
            result = result + item.toString()

        return result


class SubjectManager:
    def __init__(self, limit: float):
        self.projects = []
        self.limit = limit

    def load_project(self):
        # 差旅费：1万，会议费：1万，印刷费：0.5万，劳务费：0.5万，办公费：0.5万，咨询费：0.6万，专用材料费：0.5万，
        # 委托业务费：0.2万，邮电费的具体金额：0.2万
        project = Project(self.limit)
        project.addItem(Item("差旅费", 1))
        project.addItem(Item("会议费", 1))
        project.addItem(Item("印刷费", 0.5))
        project.addItem(Item("劳务费", 0.5))
        project.addItem(Item("办公费", 0.5))
        project.addItem(Item("咨询费", 0.6))
        project.addItem(Item("邮电费", 0.2))
        project.addItem(Item("专用材料费", 0.5))
        project.addItem(Item("委托业务费", 0.2))
        self.projects.append(project)
        #差旅费：0.25万，劳务费：0.2万，咨询费：0.2万，办公费：0.05万
        project = Project(self.limit)
        project.addItem(Item("差旅费", 0.25))
        project.addItem(Item("会议费", 0))
        project.addItem(Item("印刷费", 0))
        project.addItem(Item("劳务费", 0.2))
        project.addItem(Item("办公费", 0.05))
        project.addItem(Item("咨询费", 0.2))
        project.addItem(Item("邮电费", 0))
        project.addItem(Item("专用材料费", 0))
        project.addItem(Item("委托业务费", 0))
        self.projects.append(project)

        # 差旅费：0.3万，印刷费：0.05万，咨询费：0.1万，办公费：0.05万
        project = Project(self.limit)
        project.addItem(Item("差旅费", 0.3))
        project.addItem(Item("会议费", 0))
        project.addItem(Item("印刷费", 0.05))
        project.addItem(Item("劳务费", 0.0))
        project.addItem(Item("办公费", 0.05))
        project.addItem(Item("咨询费", 0.1))
        project.addItem(Item("邮电费", 0))
        project.addItem(Item("专用材料费", 0))
        project.addItem(Item("委托业务费", 0))
        self.projects.append(project)

        # 差旅费：0.3万，会议费：0.1万，印刷费：0.5万，办公费：0.05万，专用材料费：0.05万
        project = Project(self.limit)
        project.addItem(Item("差旅费", 0.3))
        project.addItem(Item("会议费", 0.1))
        project.addItem(Item("印刷费", 0.5))
        project.addItem(Item("劳务费", 0.0))
        project.addItem(Item("办公费", 0.05))
        project.addItem(Item("咨询费", 0))
        project.addItem(Item("邮电费", 0))
        project.addItem(Item("专用材料费", 0.05))
        project.addItem(Item("委托业务费", 0))
        self.projects.append(project)

        # 差旅费：1.2万，印刷费：1.2万，咨询费：0.6万，专用材料费：1.2万，委托业务费：0.8万
        project = Project(self.limit)
        project.addItem(Item("差旅费", 1.2))
        project.addItem(Item("会议费", 0))
        project.addItem(Item("印刷费", 1.2))
        project.addItem(Item("劳务费", 0.0))
        project.addItem(Item("办公费", 0))
        project.addItem(Item("咨询费", 0.6))
        project.addItem(Item("邮电费", 0))
        project.addItem(Item("专用材料费", 1.2))
        project.addItem(Item("委托业务费", 0.8))
        self.projects.append(project)

        # 差旅费：1.2万，印刷费：0.05万，劳务费：0.2万，咨询费：0.2万，委托业务费：0.35万
        project = Project(self.limit)
        project.addItem(Item("差旅费", 1.2))
        project.addItem(Item("会议费", 0))
        project.addItem(Item("印刷费", 0.05))
        project.addItem(Item("劳务费", 0.2))
        project.addItem(Item("办公费", 0))
        project.addItem(Item("咨询费", 0.2))
        project.addItem(Item("邮电费", 0))
        project.addItem(Item("专用材料费", 0))
        project.addItem(Item("委托业务费", 0.35))
        self.projects.append(project)

        # 差旅费：0.9万，印刷费：0.1万
        project = Project(self.limit)
        project.addItem(Item("差旅费", 0.9))
        project.addItem(Item("会议费", 0))
        project.addItem(Item("印刷费", 0.1))
        project.addItem(Item("劳务费", 0.0))
        project.addItem(Item("办公费", 0))
        project.addItem(Item("咨询费", 0.0))
        project.addItem(Item("邮电费", 0))
        project.addItem(Item("专用材料费", 0))
        project.addItem(Item("委托业务费", 0.0))
        self.projects.append(project)

        # 差旅费：0.6万，会议费：0.3万，印刷费：0.35万，劳务费：0.2万，办公费：0.05万
        project = Project(self.limit)
        project.addItem(Item("差旅费", 0.6))
        project.addItem(Item("会议费", 0.3))
        project.addItem(Item("印刷费", 0.35))
        project.addItem(Item("劳务费", 0.2))
        project.addItem(Item("办公费", 0.05))
        project.addItem(Item("咨询费", 0.0))
        project.addItem(Item("邮电费", 0))
        project.addItem(Item("专用材料费", 0))
        project.addItem(Item("委托业务费", 0.0))
        self.projects.append(project)

        # 会议费：0.4万，劳务费：0.8万，委托业务费：0.8万
        project = Project(self.limit)
        project.addItem(Item("差旅费", 0.0))
        project.addItem(Item("会议费", 0.4))
        project.addItem(Item("印刷费", 0.0))
        project.addItem(Item("劳务费", 0.8))
        project.addItem(Item("办公费", 0.0))
        project.addItem(Item("咨询费", 0.0))
        project.addItem(Item("邮电费", 0))
        project.addItem(Item("专用材料费", 0))
        project.addItem(Item("委托业务费", 0.8))
        self.projects.append(project)

        # 差旅费：0.5万，印刷费：0.5万，劳务费：0.6万，办公用品费：0.3万，图书资料费：0.1万
        project = Project(self.limit)
        project.addItem(Item("差旅费", 0.5))
        project.addItem(Item("会议费", 0.0))
        project.addItem(Item("印刷费", 0.6))
        project.addItem(Item("劳务费", 0.6))
        project.addItem(Item("办公费", 0.3))
        project.addItem(Item("咨询费", 0.0))
        project.addItem(Item("邮电费", 0))
        project.addItem(Item("专用材料费", 0))
        project.addItem(Item("委托业务费", 0.0))
        self.projects.append(project)

        # 差旅费：0.7万，委托业务费：0.3万
        project = Project(self.limit)
        project.addItem(Item("差旅费", 0.7))
        project.addItem(Item("会议费", 0.0))
        project.addItem(Item("印刷费", 0.0))
        project.addItem(Item("劳务费", 0.0))
        project.addItem(Item("办公费", 0.0))
        project.addItem(Item("咨询费", 0.0))
        project.addItem(Item("邮电费", 0))
        project.addItem(Item("专用材料费", 0))
        project.addItem(Item("委托业务费", 0.3))
        self.projects.append(project)

        # 差旅费：0.3万，印刷费：0.1万，咨询费：0.1万，专用材料费：0.2万
        project = Project(self.limit)
        project.addItem(Item("差旅费", 0.3))
        project.addItem(Item("会议费", 0.0))
        project.addItem(Item("印刷费", 0.1))
        project.addItem(Item("劳务费", 0.0))
        project.addItem(Item("办公费", 0.0))
        project.addItem(Item("咨询费", 0.1))
        project.addItem(Item("邮电费", 0))
        project.addItem(Item("专用材料费", 0.2))
        project.addItem(Item("委托业务费", 0.0))
        self.projects.append(project)

        # 差旅费：0.2万，印刷费：0.1万，办公费：0.2万，专用材料费：0.5万，委托业务费：0.5万
        project = Project(self.limit)
        project.addItem(Item("差旅费", 0.2))
        project.addItem(Item("会议费", 0.0))
        project.addItem(Item("印刷费", 0.1))
        project.addItem(Item("劳务费", 0.0))
        project.addItem(Item("办公费", 0.2))
        project.addItem(Item("咨询费", 0.0))
        project.addItem(Item("邮电费", 0))
        project.addItem(Item("专用材料费", 0.5))
        project.addItem(Item("委托业务费", 0.5))
        self.projects.append(project)

        # 差旅费：2万，印刷费：0.4万，劳务费：1万，咨询费：0.5万，专用材料费：0.8万
        project = Project(self.limit)
        project.addItem(Item("差旅费", 2))
        project.addItem(Item("会议费", 0.0))
        project.addItem(Item("印刷费", 0.4))
        project.addItem(Item("劳务费", 1))
        project.addItem(Item("办公费", 0))
        project.addItem(Item("咨询费", 0.5))
        project.addItem(Item("邮电费", 0))
        project.addItem(Item("专用材料费", 0.8))
        project.addItem(Item("委托业务费", 0))
        self.projects.append(project)

        # 资料费：0.4万，会议费：0.3万，劳务费：0.2万，印刷费：0.1万
        project = Project(self.limit)
        project.addItem(Item("差旅费", 0))
        project.addItem(Item("会议费", 0.3))
        project.addItem(Item("印刷费", 0.1))
        project.addItem(Item("劳务费", 0.2))
        project.addItem(Item("办公费", 0))
        project.addItem(Item("咨询费", 0.5))
        project.addItem(Item("邮电费", 0))
        project.addItem(Item("专用材料费", 0.0))
        project.addItem(Item("委托业务费", 0))
        self.projects.append(project)

        # 差旅费：0.3万，论文版面费：0.1万，印刷费：0.3万，劳务费：0.1万，专家咨询费：0.2万
        project = Project(self.limit)
        project.addItem(Item("差旅费", 0.3))
        project.addItem(Item("会议费", 0))
        project.addItem(Item("印刷费", 0.3))
        project.addItem(Item("劳务费", 0.1))
        project.addItem(Item("办公费", 0))
        project.addItem(Item("咨询费", 0.2))
        project.addItem(Item("邮电费", 0))
        project.addItem(Item("专用材料费", 0.0))
        project.addItem(Item("委托业务费", 0.1))
        self.projects.append(project)

        # 差旅费：0.8万，印刷费：1.2万，劳务费：0.5万，办公费：0.2万，咨询费：1万，邮电费：0.1万，委托业务费：0.2万
        project = Project(self.limit)
        project.addItem(Item("差旅费", 0.8))
        project.addItem(Item("会议费", 0))
        project.addItem(Item("印刷费", 1.2))
        project.addItem(Item("劳务费", 0.5))
        project.addItem(Item("办公费", 0.2))
        project.addItem(Item("咨询费", 1))
        project.addItem(Item("邮电费", 0.1))
        project.addItem(Item("专用材料费", 0.0))
        project.addItem(Item("委托业务费", 0.2))
        self.projects.append(project)

        # 差旅费：1.8万，印刷费：1.2万，劳务费：0.6万，办公费：0.3万，咨询费：1万，邮电费：0.1万，租赁费：0.8万，委托业务费：0.2万
        project = Project(self.limit)
        project.addItem(Item("差旅费", 1.8))
        project.addItem(Item("会议费", 0))
        project.addItem(Item("印刷费", 1.2))
        project.addItem(Item("劳务费", 0.6))
        project.addItem(Item("办公费", 0.3))
        project.addItem(Item("咨询费", 1))
        project.addItem(Item("邮电费", 0.1))
        project.addItem(Item("专用材料费", 0.8))
        project.addItem(Item("委托业务费", 0.2))
        self.projects.append(project)

        # 差旅费：1万，印刷费：0.6万，劳务费：0.3万，办公费：0.5万，咨询费：0.2万，委托业务费：0.3万，其他：0.1万
        project = Project(self.limit)
        project.addItem(Item("差旅费", 1))
        project.addItem(Item("会议费", 0))
        project.addItem(Item("印刷费", 0.6))
        project.addItem(Item("劳务费", 0.3))
        project.addItem(Item("办公费", 0.5))
        project.addItem(Item("咨询费", 0.2))
        project.addItem(Item("邮电费", 0))
        project.addItem(Item("专用材料费", 0.1))
        project.addItem(Item("委托业务费", 0.3))
        self.projects.append(project)

        # 差旅费：3.5万，印刷费：1万，劳务费：0.5万
        project = Project(self.limit)
        project.addItem(Item("差旅费", 3.5))
        project.addItem(Item("会议费", 0))
        project.addItem(Item("印刷费", 1))
        project.addItem(Item("劳务费", 0.5))
        project.addItem(Item("办公费", 0))
        project.addItem(Item("咨询费", 0))
        project.addItem(Item("邮电费", 0))
        project.addItem(Item("专用材料费", 0))
        project.addItem(Item("委托业务费", 0))
        self.projects.append(project)

        # 印刷费0.3万，差旅费和会议费0.3万，劳务费0.3万，咨询费0.1万
        project = Project(self.limit)
        project.addItem(Item("差旅费", 0.2))
        project.addItem(Item("会议费", 0.1))
        project.addItem(Item("印刷费", 0.3))
        project.addItem(Item("劳务费", 0.3))
        project.addItem(Item("办公费", 0))
        project.addItem(Item("咨询费", 0.1))
        project.addItem(Item("邮电费", 0))
        project.addItem(Item("专用材料费", 0))
        project.addItem(Item("委托业务费", 0))
        self.projects.append(project)

        # 差旅费0.8万，印刷费1万，办公设备0.2万
        project = Project(self.limit)
        project.addItem(Item("差旅费", 0.8))
        project.addItem(Item("会议费", 0))
        project.addItem(Item("印刷费", 1))
        project.addItem(Item("劳务费", 0))
        project.addItem(Item("办公费", 0.2))
        project.addItem(Item("咨询费", 0))
        project.addItem(Item("邮电费", 0))
        project.addItem(Item("专用材料费", 0))
        project.addItem(Item("委托业务费", 0))
        self.projects.append(project)

    def print(self):
        with open("log.txt", "a") as f:
            for project in self.projects:
                result = project.toString()
                print(result)
                f.write("总计：" + str(round(project.sum(),2)) + "万元 " + result + "\n")

    def collect(self):
        print("-------------统计信息-------------")
        info = dict()
        for project in self.projects:
            for item in project.items:
                if item.name in info.keys():
                    info[item.name] = info[item.name]+item.value
                else:
                    info[item.name] = item.value
        result = ""
        sum = 0
        for key in info.keys():
            sum = sum + info[key]
            result = result +(str(key) + "：" + str(round(info[key],2)) + "万元，")
        result = result + "共" + str(len(self.projects)) + "个项目，"
        result = result + "总计：" + str(round(sum, 2)) + "万元"
        print(result)

class ReformManager(SubjectManager):
    def __init__(self, limit: float = 2.5):
        super().__init__(limit)
        # self.limit = 2.5

    def load_project(self):
        # 设备费：0.5万，资料费：0.5万，会议费：0.5万，差旅费：1万，国际合作与交流费：0.5万
        project = Project(self.limit)
        project.addItem(Item("差旅费", 1))
        project.addItem(Item("会议费", 0.5))
        project.addItem(Item("印刷费", 0.5))
        project.addItem(Item("劳务费", 0.0))
        project.addItem(Item("办公费", 0.0))
        project.addItem(Item("咨询费", 0.5))
        project.addItem(Item("邮电费", 0.0))
        project.addItem(Item("专用材料费", 0.5))
        project.addItem(Item("委托业务费", 0.0))
        self.projects.append(project)

        # 差旅费：0.3万，论文版面费：0.1万，印刷费：0.3万，劳务费：0.1万，专家咨询费：0.2万
        project = Project(self.limit)
        project.addItem(Item("差旅费", 0.3))
        project.addItem(Item("会议费", 0.0))
        project.addItem(Item("印刷费", 0.3))
        project.addItem(Item("劳务费", 0.1))
        project.addItem(Item("办公费", 0.0))
        project.addItem(Item("咨询费", 0.2))
        project.addItem(Item("邮电费", 0.0))
        project.addItem(Item("专用材料费", 0.0))
        project.addItem(Item("委托业务费", 0.1))
        self.projects.append(project)

        # 外出交流研讨（差旅）：13000，相关教育教学类研究论文发表：4000，参加MPA教育教学培训费用：3000，电脑耗材及课程网站建设等：3000，专家咨询费：4000，学生劳务费：3000
        project = Project(self.limit)
        project.addItem(Item("差旅费", 1.3))
        project.addItem(Item("会议费", 0.0))
        project.addItem(Item("印刷费", 0.0))
        project.addItem(Item("劳务费", 0.3))
        project.addItem(Item("办公费", 0.3))
        project.addItem(Item("咨询费", 0.4))
        project.addItem(Item("邮电费", 0.0))
        project.addItem(Item("专用材料费", 0.0))
        project.addItem(Item("委托业务费", 0.7))
        self.projects.append(project)

        # 差旅费：0.9万，打印复印费：0.3万，论文版面费：0.3万，办公材料费：0.5万
        project = Project(self.limit)
        project.addItem(Item("差旅费", 0.9))
        project.addItem(Item("会议费", 0.0))
        project.addItem(Item("印刷费", 0.3))
        project.addItem(Item("劳务费", 0.0))
        project.addItem(Item("办公费", 0.5))
        project.addItem(Item("咨询费", 0.0))
        project.addItem(Item("邮电费", 0.0))
        project.addItem(Item("专用材料费", 0.0))
        project.addItem(Item("委托业务费", 0.3))
        self.projects.append(project)

        # 两次实习费：6.0万，研究生教学改革会议： 3.2万，购买图书、笔记本等用品：0.8万
        project = Project(self.limit)
        project.addItem(Item("差旅费", 6.0))
        project.addItem(Item("会议费", 3.2))
        project.addItem(Item("印刷费", 0.0))
        project.addItem(Item("劳务费", 0.0))
        project.addItem(Item("办公费", 0.8))
        project.addItem(Item("咨询费", 0.0))
        project.addItem(Item("邮电费", 0.0))
        project.addItem(Item("专用材料费", 0.0))
        project.addItem(Item("委托业务费", 0.0))
        self.projects.append(project)

        # 版面费：0.4万，差旅费：0.3万，会议费：0.3万
        project = Project(self.limit)
        project.addItem(Item("差旅费", 0.3))
        project.addItem(Item("会议费", 0.3))
        project.addItem(Item("印刷费", 0.0))
        project.addItem(Item("劳务费", 0.0))
        project.addItem(Item("办公费", 0.0))
        project.addItem(Item("咨询费", 0.0))
        project.addItem(Item("邮电费", 0.0))
        project.addItem(Item("专用材料费", 0.0))
        project.addItem(Item("委托业务费", 0.4))
        self.projects.append(project)

        # 办公费：0.9万，印刷费：2.2万，差旅费：3.5万，劳务费：3.4万
        project = Project(self.limit)
        project.addItem(Item("差旅费", 3.5))
        project.addItem(Item("会议费", 0.0))
        project.addItem(Item("印刷费", 2.2))
        project.addItem(Item("劳务费", 3.4))
        project.addItem(Item("办公费", 0.9))
        project.addItem(Item("咨询费", 0.0))
        project.addItem(Item("邮电费", 0.0))
        project.addItem(Item("专用材料费", 0.0))
        project.addItem(Item("委托业务费", 0.4))
        self.projects.append(project)

        #
        project = Project(self.limit)
        project.addItem(Item("差旅费", 1.2))
        project.addItem(Item("会议费", 0.0))
        project.addItem(Item("印刷费", 2.2))
        project.addItem(Item("劳务费", 3.4))
        project.addItem(Item("办公费", 0.9))
        project.addItem(Item("咨询费", 0.0))
        project.addItem(Item("邮电费", 0.0))
        project.addItem(Item("专用材料费", 0.0))
        project.addItem(Item("委托业务费", 0.4))
        self.projects.append(project)

        # 教材或讲义出版：1.5万，图书资料购置：0.2万，会议培训：0.3万
        project = Project(self.limit)
        project.addItem(Item("差旅费", 0))
        project.addItem(Item("会议费", 0.3))
        project.addItem(Item("印刷费", 0.2))
        project.addItem(Item("劳务费", 0))
        project.addItem(Item("办公费", 0.0))
        project.addItem(Item("咨询费", 0.0))
        project.addItem(Item("邮电费", 0.0))
        project.addItem(Item("专用材料费", 0.0))
        project.addItem(Item("委托业务费", 1.5))
        self.projects.append(project)

        # 书籍、文献资料购买费用：0.2万，打印、复印资料费：0.05万，调研程序制作费：0.02万，外籍老师劳务费：0.13万，学生劳务费：0.1万
        project = Project(self.limit)
        project.addItem(Item("差旅费", 0))
        project.addItem(Item("会议费", 0.0))
        project.addItem(Item("印刷费", 0.25))
        project.addItem(Item("劳务费", 0.23))
        project.addItem(Item("办公费", 0.0))
        project.addItem(Item("咨询费", 0.0))
        project.addItem(Item("邮电费", 0.02))
        project.addItem(Item("专用材料费", 0.0))
        project.addItem(Item("委托业务费", 0))
        self.projects.append(project)

        # 办公用品，电脑相关用品；支付打印复印费、专家咨询费、参加教学研讨会
        project = Project(self.limit)
        project.addItem(Item("差旅费", 0.2))
        project.addItem(Item("会议费", 0.0))
        project.addItem(Item("印刷费", 0.25))
        project.addItem(Item("劳务费", 0.23))
        project.addItem(Item("办公费", 0.0))
        project.addItem(Item("咨询费", 0.0))
        project.addItem(Item("邮电费", 0.02))
        project.addItem(Item("专用材料费", 0.0))
        project.addItem(Item("委托业务费", 0))
        self.projects.append(project)

        # 差旅费：1万，材料费：0.1万，服务器租赁费：0.6万
        project = Project(self.limit)
        project.addItem(Item("差旅费", 1))
        project.addItem(Item("会议费", 0.0))
        project.addItem(Item("印刷费", 0.1))
        project.addItem(Item("劳务费", 0.0))
        project.addItem(Item("办公费", 0.0))
        project.addItem(Item("咨询费", 0.0))
        project.addItem(Item("邮电费", 0.0))
        project.addItem(Item("专用材料费", 0.6))
        project.addItem(Item("委托业务费", 0))
        self.projects.append(project)




if __name__=='__main__':
    # 教改2.5， 精品课1.5
    """
    #
    project = Project(2.5)
    project.addItem(Item("差旅费", 0))
    project.addItem(Item("会议费", 0))
    project.addItem(Item("印刷费", 0))
    project.addItem(Item("劳务费", 0))
    project.addItem(Item("办公费", 0))
    project.addItem(Item("咨询费", 0))
    project.addItem(Item("邮电费", 0))
    project.addItem(Item("专用材料费", 0))
    project.addItem(Item("委托业务费", 0))
    self.projects.append(project)
    with open("log.txt", "a") as f:
        result = project.toString()
        print(result)
        f.write(result+"\n")
    """
    manager = SubjectManager(1.5)
    manager.load_project()
    manager.print()
    manager.collect()